/**
 * Copyright (C), 2016-2016
 * Date:     2016-02-24
 * Description: promotionInfo的交互效果（留资弹窗）
 */

//发送到手机
function sendToMobile(orderId){
	var url = "sendToMobile.htm";
	$.post(url,{"preOrderId":orderId},function(data){
		if(data=='success'){
			alert("发送成功");
		}else{
			alert("网络故障,发送失败");
		}
	});
}
    
//查询
function searchPromotion(){
	var selectValue = $("#selectPromotion").val();
	//$("#trialDriveForm").submit();
	var currentPageNo = currentPageNo;
	if(selectValue == '0'){
		selectValue = null;
	}
	$.ajax({
            cache: true,
            type: "POST",
            url: "promotionList.htm",
            //data:$('#trialDriveForm').serialize(),
            data:{marketType:selectValue,currentPage:currentPageNo},
            async: false,
            error: function(request) {
                alert("Connection error");
            },
            success: function(data) {
                $("#product").html(data);
            }
        });
}
    
//分页处理
function goPage(currentPageNo){
    var selectValue = $("#selectPromotion").val();
	//if(selectValue == '0'){
	//	selectValue = null;
	//}
	$.ajax({
		url:"promotionList.htm",
		type: "POST",
		data:{
			currentPage:currentPageNo,
			marketType:selectValue
		},
		success: function(data){
        	$("#product").html(data);
        }
	});
}
		
//查看秒杀订单
 function openActivity(obj){
	window.open(obj);
	//window.location.href=obj;
}
//整车秒杀跳转
function leaveMsgCar(pid,subId){
	$.ajax({
		url:base+'/member/promotion/generateOrderUrl.htm',
		type:'POST',
		data:'promotionId='+pid+'&subscriptionId='+subId,
		dataType:'json',
		success:function(data){
			var result = data;
			if(result.result==true){
				ECar.dialog.alert('您已经提交过提车信息，请不要重复操作');
			}else{
				window.location.href=result.result;
			}
		}
	});
}

//查询当前用户信息，便于留资（附件）
function leaveMsg(pid,subId){
	var urlpath = base+'/member/promotion/secKillAddressLoad_'+pid+'_'+subId+'.htm';
	var dataval = 'promotionId='+pid+'&subscriptionId='+subId;
	$.ajax({
		url:urlpath,
		type:'POST',
		data:dataval,
		dataType:'json',
		success:function(data){
			var result = data ;
			if(result.code==true){
				ECar.dialog.alert('您已经提交过收货信息，请不要重复操作');
			}else{
				var customerInfo = result.customerInfo;
				//$("#receiverName").val(customerInfo.receiverName);
				$("#telephone").val(customerInfo.telephone);
				$("#custProvinceId").val(customerInfo.custProvinceId);
				$("#custCityId").val(customerInfo.custCityId);
				$("#address").val(customerInfo.address);
				$("#postCode").val(customerInfo.postCode);
				$("#promotionId").val(customerInfo.promotionId);
				$("#subscriptionId").val(customerInfo.subscriptionId);
				popupWin("seckill-present");
			}
		}
	});
}

function openOrder(obj){
	$.ajax({
		url:base+'/member/promotion/toCarOrder.htm',
		type:'POST',
		data:'promotionId='+obj ,
		dataType:'json',
		success:function(data){
			window.location.href=base+"/member/orderDetail.htm?orderId="+data.orderId;
		}
	});
}
	
//留资提交前验证
function submitForm(){
	var checkError = false;
	var showErrorMessage ='';
	//closeWin("errorMessage");
	$("#message").html('');
	if($.trim($("#receiverName").val()) == ""){
		showErrorMessage =  showErrorMessage+'请输入姓名</br>';
		checkError = true;
	} else if( $.trim($("#receiverName").val()).length > 10 ){
		showErrorMessage =  showErrorMessage+'姓名格式有误</br>';
		checkError = true;
	}
	
	if($.trim($("#telephone").val())==""){
		showErrorMessage =  showErrorMessage+'请输入手机号码</br>';
		checkError = true;
	} else if( !telephoneCheck($.trim($("#telephone").val())) ){
		showErrorMessage =  showErrorMessage+'手机号码有误，请核对</br>';
		checkError = true;
	}
	
	
	if($.trim($("#custCityId").val()) < 1){
		showErrorMessage =  showErrorMessage+'请选择地区</br>';
		checkError = true;
	}
	if($.trim($("#address").val())=="" || $.trim($("#address").val()).length > 99){
		showErrorMessage =  showErrorMessage+'收货地址不能为空,并且小于100位</br>';
		checkError = true;
	}
	
	if($.trim($("#postCode").val())=="" ){
		showErrorMessage =  showErrorMessage+'请输入邮政编码</br>';
		checkError = true;
	} else if( !postcodeCheck($.trim($("#postCode").val())) ){
		showErrorMessage =  showErrorMessage+'邮政编码有误，请核对</br>';
		checkError = true;
	}
	
	if(!checkError){
		//$("#addressForm").submit();
		saveAddress();
		ECar.dialog.close();
	}else{
		//ECar.dialog.close();
		ECar.dialog.alert(showErrorMessage);
	}
}
//保存收货地址
function saveAddress(){
	$.ajax({
		url:base+'/member/promotion/secKillAddressInsert.htm',
		type:'POST',
		data:'promotionId='+$("#promotionId").val()+'&subscriptionId='+$("#subscriptionId").val() + '&postCode='+$("#postCode").val()+'&address='+$("#address").val()+'&custProvinceId='+$("#custProvinceId").val() + '&custCityId='+$("#custCityId").val()+'&telephone='+$("#telephone").val() + '&receiverName='+$("#receiverName").val(),
		dataType:'json',
		success:function(data){
			var result = data	;
			if(result.code==true){
				ECar.dialog.alert('保存成功！');
			}else{
				ECar.dialog.alert('保存异常！');
			}
			location.reload();
		}
	});
}
//验证手机
function telephoneCheck(telphone){
	var re= /^1\d{10}/ ;
	if(re.test(telphone)){
		return true;
	}else{
		return false;
	}
}
//验证邮箱 
function emailCheck(email){
	var re= /\w@\w*\.\w/;
	if(re.test(email)){
		return true;
	}else{
		return false;
	}
}
//验证邮编
function postcodeCheck(postcode){
	var re= /^\d{6}$/;
	if(re.test(postcode)){
		return true;
	}else{
		return false;
	}
}
//错误提示窗口关闭事件
$("#errorClose").on("click",function(){
	$("#errorMessage").hide();
});
function popupWin(obj){
	ECar.dialog.open({
		title:'实物礼品邮寄信息',
		//content:$('#seckill-present').html(),
		id: "seckill-present",
		mode: "id",
		height:450,
		width:600,
		top:50
	});
}

$("#submitForm").bind("click",submitForm);
//获取省市
ECar.seriesSelect($('#member_city'), {
	selects: ["member_province", "member_city_class", "member_area_class"],
	url: base+'/member/getAllLinkageData.htm',
	nodata: '',
	complete:function(){
		var val = $(".member_province").val();    
	}
});	


/*-------------------------------20160224-------------------------------------*/
//验证身份证 20160224
function identityCheck(identity){
	var re= /^(\d{18}$|^\d{17}(\d|X|x))$/;
	if(re.test(identity)){
		return true;
	}else{
		return false;
	}
}
//验证银行卡号 20160224
function bankCheck(identity){
	var re= /^\d{16}|\d{19}$/;
	if(re.test(identity)){
		return true;
	}else{
		return false;
	}
}
//验证用户名 20160303
function nameCheck(identity){
	var re= /^[A-Za-z\-\u4e00-\u9fa5]+$/;
	if(re.test(identity)){
		return true;
	}else{
		return false;
	}
}

//328保存收货地址
function saveUserInfo(param){
	var userName = $('#receiverName').val() || $('#insureName').val();
	var userId = $('#receiverCode').val() || $('#insureCode').val();
	
	if( userName.length > 0 && userId.length > 0 ){
		$.ajax({
			url:base+'/member/checkUserCard.htm',
			type:'POST',
			data:{'userName':userName,'idNumber':userId},
			dataType:'json',
			success:function(data){
				switch(data){
					case 200:
						saveInfo();
						break;
					case 201:
						saveInfo();
						break;
					case 202:
						alert("您填写的信息有误请仔细核对");
						break;
					case 203:
						alert("您填写的信息有误请仔细核对");
						break;
					case 204:
						alert("您填写的信息有误请仔细核对");
						break;
					case 205:
						alert("操作过于频繁，请勿重复提交");
						break;
					case 206:
						alert("系统繁忙，请稍后再试");
						break;
					case 207:
						alert("您填写的信息有误请仔细核对");
						break;
					default:
						alert("系统繁忙，请稍后再试");
						break;
				}
            },
			error:function(){
				alert("您填写的信息有误请仔细核对");
			}
		});
	} else {
		saveInfo();
	}
	/*//PRE环境么有身份证校验接口 先注释
	saveInfo();*/
	
	function saveInfo(){
		$.ajax({
			url:base+'/member/saveOrUpdateUserInfo.htm',
			type:'POST',
			data:param,
			dataType:'json',
			success:function(data){
				if(data==0){
					ECar.dialog.alert('提交成功！');
				}else{
					ECar.dialog.alert('提交异常！');
				}
				ECar.dialog.closeCallback = function(){
					location.reload();
				}
			}
		});
	}
}


//20160224 保险中奖信息弹窗
function insureWin(obj){
	ECar.dialog.open({
		title:'保险领取信息',
		id: "insure-present",
		mode: "id",
		height:290,
		width:480,
		top:50
	});
}
//20160224 保险中奖信息提交前验证
function submitFormInsure(){
	var checkError = false;
	var showErrorMessage ='';
	$("#message").html('');
	if($.trim($("#insureName").val()) == ""){
		showErrorMessage =  showErrorMessage+'请输入姓名</br>';
		checkError = true;
	} else if( $.trim($("#insureName").val()).length > 10  || !nameCheck($.trim($("#insureName").val())) ){
		showErrorMessage =  showErrorMessage+'姓名格式有误</br>';
		checkError = true;
	}
	
	if($.trim($("#insureCode").val())=="" ){
		showErrorMessage =  showErrorMessage+'请输入身份证号码</br>';
		checkError = true;
	} else if( !identityCheck($.trim($("#insureCode").val())) ){
		showErrorMessage =  showErrorMessage+'身份证号码有误，请核对</br>';
		checkError = true;
	}
	
	if($.trim($("#insureMobile").val())==""){
		showErrorMessage =  showErrorMessage+'请输入手机号码</br>';
		checkError = true;
	} else if( !telephoneCheck($.trim($("#insureMobile").val())) ){
		showErrorMessage =  showErrorMessage+'手机号码有误，请核对</br>';
		checkError = true;
	}
	
	if(!checkError){
		var param={};
			param['userName']  = $("#insureName").val();
			param['idNum']  = $("#insureCode").val();
			param['userCell']  = $("#insureMobile").val();
			param['promotionId']  = $("#promotionId").val();
			param['userInfoType']  = $("#userInfoType").val();
			param['topicCode']  = $("#topicCode").val();
			param['infoId']  = $("#infoId").val();
			param['subscriptionId']  = $("#subscriptionId").val();
			param['orderId']  = $("#orderId").val();
		saveUserInfo(param);
		ECar.dialog.close();
	}else{
		ECar.dialog.alert(showErrorMessage);
	}
}
$("#submitInsure").bind("click",submitFormInsure);

//保险中奖信息是否是编辑
$("#modifyInsure").on("click", function(){
	//看是否有数据
	$('#insureName').val("").attr("disabled",false);
	$('#insureCode').val("").attr("disabled",false);
	$('#insureMobile').val("").attr("disabled",false);
});

function insurePresent(o){
	insureWin("insure-present");

	$('#promotionId').val($(o).attr('promotionId'));
	$('#userInfoType').val($(o).attr('userInfoType'));
	$('#topicCode').val($(o).attr('topicCode'));
	$('#infoId').val($(o).attr('infoId'));
	$('#subscriptionId').val($(o).attr('subscriptionId'));
	$('#orderId').val($(o).attr('orderId'));
	
	//看是否有数据
	if( $(o).hasClass("data-done") ){
		//验证userinfotype
		if( $(o).attr("userInfoType").length > 0 ){
			//重置
			$('#insureName').val("");
			$('#insureCode').val("");
			$('#insureMobile').val("");
			//重置 End
			
			//回显
			$('#insureName').val($(o).attr('userName')).attr("disabled",true);
			$('#insureCode').val($(o).attr('idNum')).attr("disabled",true);
			$('#insureMobile').val($(o).attr('userCell')).attr("disabled",true);
		}
	} else {
		$('#insureName').val("").attr("disabled",false);
		$('#insureCode').val("").attr("disabled",false);
		$('#insureMobile').val("").attr("disabled",false);
	}
	
}
//$(".insure-present").bind("click",function(){
//	insureWin("insure-present");
//	//验证userinfotype
//	if( $(this).attr("userinfotype").length > 0 ){
//		$('#promotionId').val($(this).attr('promotionId'));
//		$('#userInfoType').val($(this).attr('userInfoType'));
//		$('#topicCode').val($(this).attr('topicCode'));
//	}
//});


//20160227 礼品邮寄地址提交前验证
function submitGood(){
	var checkError = false;
	var showErrorMessage ='';
	$("#message").html('');
	if($.trim($("#receiverName").val()) == ""){
		showErrorMessage =  showErrorMessage+'请输入姓名</br>';
		checkError = true;
	} else if( $.trim($("#receiverName").val()).length > 10  || !nameCheck($.trim($("#receiverName").val())) ){
		showErrorMessage =  showErrorMessage+'姓名格式有误</br>';
		checkError = true;
	}
	
	if($.trim($("#receiverCode").val())=="" ){
		showErrorMessage =  showErrorMessage+'请输入身份证号码</br>';
		checkError = true;
	} else if( !identityCheck($.trim($("#receiverCode").val())) ){
		showErrorMessage =  showErrorMessage+'身份证号码有误，请核对</br>';
		checkError = true;
	}
	
	if($.trim($("#telephone").val())==""){
		showErrorMessage =  showErrorMessage+'请输入手机号码</br>';
		checkError = true;
	} else if( !telephoneCheck($.trim($("#telephone").val())) ){
		showErrorMessage =  showErrorMessage+'手机号码有误，请核对</br>';
		checkError = true;
	}
	
	
	if($.trim($("#custCityId").val()) < 1 && $('#spanData').attr('custCityId') < 1){
		showErrorMessage =  showErrorMessage+'请选择市</br>';
		checkError = true;
	}
	
	if($.trim($("#custAeraId").val()) < 1 && $('#spanData').attr('custAeraId') < 1){
		showErrorMessage =  showErrorMessage+'请选择地区</br>';
		checkError = true;
	}
	
	if($.trim($("#address").val())==""){
		showErrorMessage =  showErrorMessage+'请输入详细地址</br>';
		checkError = true;
	} else if( $.trim($("#address").val()).length > 199 ){
		showErrorMessage =  showErrorMessage+'收货地址过长,不超过100字</br>';
		checkError = true;
	}
	
	if($.trim($("#postCode").val())=="" ){
		showErrorMessage =  showErrorMessage+'请输入邮政编码</br>';
		checkError = true;
	} else if( !postcodeCheck($.trim($("#postCode").val())) ){
		showErrorMessage =  showErrorMessage+'邮政编码有误，请核对</br>';
		checkError = true;
	}	
	
	if(!checkError){
		var param={};
			param['userName']  = $("#receiverName").val();
			param['idNum']  = $("#receiverCode").val();
			param['userCell']  = $("#telephone").val();
			param['cityId']  = ($("#custCityId").val() == null || $("#custCityId").val() == '') ? $('#spanData').attr('custCityId') : $("#custCityId").val();
			param['cityName']  = ($('#custCityId option:selected').text() == null || $('#custCityId option:selected').text() == '') ? $('#spanData').attr('cityName') : $('#custCityId option:selected').text();
			param['provinceId']  = ($("#custProvinceId").val() == null || $("#custProvinceId").val() == '' || $("#custProvinceId").val() == '0') ? $('#spanData').attr('custProvinceId') : $("#custProvinceId").val();
			param['provinceName']  = ($('#custProvinceId option:selected').text() == null || $('#custProvinceId option:selected').text() == '' || $('#custProvinceId option:selected').text() == '请选择') ? $('#spanData').attr('provinceName') : $('#custProvinceId option:selected').text();
			param['districtId']  = ($("#custAeraId").val() == null  || $("#custAeraId").val() == '')? $('#spanData').attr('custAeraId') : $("#custAeraId").val();
			param['districtName']  = ($('#custAeraId option:selected').text() == null || $('#custAeraId option:selected').text() == '') ? $('#spanData').attr('districtName') : $('#custAeraId option:selected').text();
			param['customerAddress']  = $("#address").val();
			param['postCode']  = $("#postCode").val();
			param['promotionId']  = $("#promotionId").val();
			param['userInfoType']  = $("#userInfoType").val();
			param['topicCode']  = $("#topicCode").val();
			param['postCode']  = $("#postCode").val();
			param['orderId']  = $("#orderId").val();
			param['infoId']  = $("#infoId").val();
			param['subscriptionId']  = $("#subscriptionId").val();
		saveUserInfo(param);
		ECar.dialog.close();
	}else{
		ECar.dialog.alert(showErrorMessage);
	}
}
$("#submitInfo").bind("click",submitGood);

//礼品邮寄地址是否是编辑
$("#modifyInfo").on("click", function(){
	$('#receiverName').val("").attr("disabled",false);
	$('#receiverCode').val("").attr("disabled",false);
	$('#telephone').val("").attr("disabled",false);
		//显示select框
	$('#custProvinceId').show();
	$('#custCityId').show();
	$('#custAeraId').show();
	$('.data-name').hide();
		//显示select框 End
	$('#address').val("").attr("disabled",false);
	$('#postCode').val("").attr("disabled",false);
});

function seckillPresent(o){
	$('#userinfotype').show();
	$('#submitForm').hide();
	$('#submitInfo').show();
	$('.pdb-main').height(520);
	
	$('#promotionId').val($(o).attr('promotionId'));
	$('#userInfoType').val($(o).attr('userInfoType'));
	$('#topicCode').val($(o).attr('topicCode'));
	$('#infoId').val($(o).attr('infoId'));
	$('#subscriptionId').val($(o).attr('subscriptionId'));
	$('#orderId').val($(o).attr('orderId'));
	
	//看是否有数据
	if( $(o).hasClass("data-done") ){
		//验证userinfotype
		if( $(o).attr("userInfoType").length > 0 ){
			//重置
			$('#receiverName').val("");
			$('#receiverCode').val("");
			$('#telephone').val("");
			$('.data-name').remove();
			$('#address').val("");
			$('#postCode').val("");
			//重置 End
			
			//回显
			$('#receiverName').val($(o).attr('userName')).attr("disabled",true);
			$('#receiverCode').val($(o).attr('idNum')).attr("disabled",true);
			$('#telephone').val($(o).attr('userCell')).attr("disabled",true);
				//隐藏select框
			$('#custProvinceId').hide();
			$('#custCityId').hide();
			$('#custAeraId').hide();
			var pName= $(o).attr('provinceName'),
				cName= $(o).attr('cityName'),
				dName= $(o).attr('districtName');
			$('#custProvinceId').before("<span id='spanData' custCityId='' cityName='' custProvinceId='' provinceName='' custAeraId='' districtName='' class='data-name'>"+pName+" "+cName+""+dName+"</span>");
			// 赋值
			$("#spanData").attr('custCityId', $(o).attr('cityId'));
			$("#spanData").attr('custProvinceId', $(o).attr('provinceId'));
			$("#spanData").attr('custAeraId', $(o).attr('districtId'));
			$('#spanData').attr('cityName', $(o).attr('cityName'));
			$('#spanData').attr('provinceName', $(o).attr('provinceName'));
			$('#spanData').attr('districtName', $(o).attr('districtName'));
			
				//隐藏select框 End
			$('#address').val($(o).attr('customerAddress')).attr("disabled",true);
			$('#postCode').val($(o).attr('postCode')).attr("disabled",true);
		}
	} else {
		$('#receiverName').val("").attr("disabled",false);
		$('#receiverCode').val("").attr("disabled",false);
		$('#telephone').val("").attr("disabled",false);
			//显示select框
		$('#custProvinceId').show();
		$('#custCityId').show();
		$('#custAeraId').show();
		$('.data-name').hide();
			//显示select框 End
		$('#address').val("").attr("disabled",false);
		$('#postCode').val("").attr("disabled",false);
	}
	
	popupWin("seckill-present");
}

//$(".seckill-present").bind("click",function(){
//	$('#userinfotype').show();
//	$('#submitForm').hide();
//	$('#submitInfo').show();
//	$('.pdb-main').height(520);
//	//验证userinfotype
//	if( $(this).attr("userinfotype").length > 0 ){
//		$('#promotionId').val($(this).attr('promotionId'));
//		$('#userInfoType').val($(this).attr('userInfoType'));
//		$('#topicCode').val($(this).attr('topicCode'));
//	}
//	popupWin("seckill-present");
//});


//20160227 保险信息弹窗（购车）
function infoWin(obj){
	ECar.dialog.open({
		title:'保险领取信息',
		id: "info-present",
		mode: "id",
		height:300,
		width:480,
		top:50
	});
}
//20160227 保险信息弹窗（购车）提交前验证
function submitInfo(){
	var checkError = false;
	var showErrorMessage ='';
	$("#message").html('');
	
	if($.trim($("#bankCode").val())=="" ){
		showErrorMessage =  showErrorMessage+'请输入您要投保的银行卡号</br>';
		checkError = true;
	} else if( !bankCheck($.trim($("#bankCode").val())) ){
		showErrorMessage =  showErrorMessage+'银行卡号有误，请核对</br>';
		checkError = true;
	}
	
	if(!checkError){
		var param={};
			param['bankAccount']  = $("#bankCode").val();
			param['promotionId']  = $("#promotionId").val();
			param['userInfoType']  = $("#userInfoType").val();
			param['topicCode']  = $("#topicCode").val();
			param['infoId']  = $("#infoId").val();
			param['subscriptionId']  = $("#subscriptionId").val();
			param['orderId']  = $("#orderId").val();
		saveUserInfo(param);
		ECar.dialog.close();
	}else{
		ECar.dialog.alert(showErrorMessage);
	}
}
$("#submitBank").bind("click",submitInfo);

//保险信息弹窗（购车）是否是编辑
$("#modifyBnak").on("click", function(){
	//看是否有数据
	$('#bankCode').val("").attr("disabled",false);
});

function infoPresent(o){
	infoWin("info-present");
	
	$('#promotionId').val($(o).attr('promotionId'));
	$('#userInfoType').val($(o).attr('userInfoType'));
	$('#topicCode').val($(o).attr('topicCode'));
	$('#infoId').val($(o).attr('infoId'));
	$('#subscriptionId').val($(o).attr('subscriptionId'));
	$('#orderId').val($(o).attr('orderId'));
	
	//看是否有数据
	if( $(o).hasClass("data-done") ){
		//验证userinfotype
		if( $(o).attr("userInfoType").length > 0 ){
			//重置
			$('#bankCode').val("");
			//重置 End
			
			//回显
			$('#bankCode').val($(o).attr('bankAccount')).attr("disabled",true);
		}
	} else {
		$('#bankCode').val("").attr("disabled",false);
	}
	
}

//$(".info-present").bind("click",function(){
//	infoWin("info-present");
//	//验证userinfotype
//	if( $(this).attr("userinfotype").length > 0 ){
//		$('#promotionId').val($(this).attr('promotionId'));
//		$('#userInfoType').val($(this).attr('userInfoType'));
//		$('#topicCode').val($(this).attr('topicCode'));
//	}
//});

